from kubernetes import client, config
from common.KubeJob import KubeJob
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'AnsibleGUI.settings')
django.setup()

from k8s.models import *

def create_job_object(obj):
    # Configureate Pod template container
    container = client.V1Container(
        name="pi",
        image="perl",
        command=["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"])
    # Create and configurate a spec section
    template = client.V1PodTemplateSpec(
        metadata=client.V1ObjectMeta(labels={"app": "pi"}),
        spec=client.V1PodSpec(restart_policy="Never", containers=[container]))
    # Create the specification of deployment
    spec = client.V1JobSpec(
        template=template,
        backoff_limit=4)
    # Instantiate the job object
    job = client.V1Job(
        api_version="batch/v1",
        kind="Job",
        metadata=client.V1ObjectMeta(name=obj.name),
        spec=spec)
    return job

if __name__ == "__main__":
    # obj = {
    #     "name": "Job11",
    #     "image": "Exporter:v1.7",
    #     "command": "['python','--version']",
    #     "weight": 0.5,
    #     "train_rate": 0.5,
    #     "type": "cpu",
    #     "resource": {
    #         "pu": 200,
    #         "ram": 200,
    #         "disk": 200
    #     }
    # }
    obj = K8sJob.objects.get(id=8)
    list = ['node01','node02']

    print(create_job_object(obj,"node01"))


